package com.ltu.ai.sparkdesk.websocket.iat;

import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
import java.util.Base64;

import org.java_websocket.client.WebSocketClient;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import com.google.gson.JsonObject;
import com.ltu.ai.sparkdesk.websocket.AiContextManager;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class IatSendHandle {
	  
//	@Async(value = "customServiceExecutor")
	public void   sendData(IatSocketClient webSocket,String filePath ){
	       //连接成功，开始发送数据
        int frameSize = 1280; //每一帧音频的大小,建议每 40ms 发送 122B
        int intervel = 40;
        int status = 0;  // 音频的状态
        try (FileInputStream fs = new FileInputStream(filePath)) {
            byte[] buffer = new byte[frameSize];
            // 发送音频
            end:
            while (true) {
                int len = fs.read(buffer);
                if (len == -1) 
                     status = 2;  //文件读完，改变status 为 2
                switch (status) {
                    case 0:   // 第一帧音频status = 0
                        JsonObject frame = new JsonObject();
                        JsonObject business = new JsonObject();  //第一帧必须发送
                        JsonObject common = new JsonObject();  //第一帧必须发送
                        JsonObject data = new JsonObject();  //每一帧都要发送
                        // 填充common
                        common.addProperty("app_id", AiContextManager.APPID);
                        //填充business
                        business.addProperty("language", "zh_cn");
                        //business.addProperty("language", "en_us");//英文
                        //business.addProperty("language", "ja_jp");//日语，在控制台可添加试用或购买
                        //business.addProperty("language", "ko_kr");//韩语，在控制台可添加试用或购买
                        //business.addProperty("language", "ru-ru");//俄语，在控制台可添加试用或购买
                        business.addProperty("domain", "iat");
                        business.addProperty("accent", "mandarin");//中文方言请在控制台添加试用，添加后即展示相应参数值
                        //business.addProperty("nunum", 0);
                        //business.addProperty("ptt", 0);//标点符号
                        //business.addProperty("rlang", "zh-hk"); // zh-cn :简体中文（默认值）zh-hk :繁体香港(若未授权不生效，在控制台可免费开通)
                        //business.addProperty("vinfo", 1);
                        business.addProperty("dwa", "wpgs");//动态修正(若未授权不生效，在控制台可免费开通)
                        //business.addProperty("nbest", 5);// 句子多候选(若未授权不生效，在控制台可免费开通)
                        //business.addProperty("wbest", 3);// 词级多候选(若未授权不生效，在控制台可免费开通)
                        //填充data
                        data.addProperty("status", 0);
                        data.addProperty("format", "audio/L16;rate=16000");
                        data.addProperty("encoding", "raw");
                        data.addProperty("audio", Base64.getEncoder().encodeToString(Arrays.copyOf(buffer, len)));
                        //填充frame
                        frame.add("common", common);
                        frame.add("business", business);
                        frame.add("data", data);
                        webSocket.send(frame.toString());
                        status = 1;  // 发送完第一帧改变status 为 1
                        break;
                    case 1:  //中间帧status = 1
                        JsonObject frame1 = new JsonObject();
                        JsonObject data1 = new JsonObject();
                        data1.addProperty("status", 1);
                        data1.addProperty("format", "audio/L16;rate=16000");
                        data1.addProperty("encoding", "raw");
                        data1.addProperty("audio", Base64.getEncoder().encodeToString(Arrays.copyOf(buffer, len)));
                        frame1.add("data", data1);
                        webSocket.send(frame1.toString());
                        break;
                    case 2:    // 最后一帧音频status = 2 ，标志音频发送结束
                        JsonObject frame2 = new JsonObject();
                        JsonObject data2 = new JsonObject();
                        data2.addProperty("status", 2);
                        data2.addProperty("audio", "");
                        data2.addProperty("format", "audio/L16;rate=16000");
                        data2.addProperty("encoding", "raw");
                        frame2.add("data", data2);
                        webSocket.send(frame2.toString());
                        System.out.println("sendlast");
                        break end;
                }
                Thread.sleep(intervel); //模拟音频采样延时
            }
          	 log.info("all data is send");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        catch (Exception e) {
        	e.printStackTrace();
        }
		
	}
}
